package com.base.context;

import com.base.helper.StringUtils;
import com.ac.interceptor.LoginInterceptor;

import java.util.Date;
import java.util.List;

/**
 * @author mzl
 * @desc
 */
public class ContextImpl implements Context {

    private String token;
    private IUser  user;
    private List<String> accessUris;
    private Date   lastAccessTime;

    public ContextImpl(final String token) {
        this.token = token;
    }

    public ContextImpl(final String token, final IUser user) {
        this.token = token;
        this.user  = user;
    }

    public ContextImpl(final String token, final IUser user, List<String> accessUris) {
        this.token = token;
        this.user  = user;
        this.accessUris = accessUris;
    }

    @Override
    public String currentToken() {
        return token;
    }

    @Override
    public IUser currentUser() {
        return user;
    }

    @Override
    public boolean isAuthorized() {
        return (user != null) && (null != user.getId()) || StringUtils.isEquals(token,
                LoginInterceptor.SWAGGER_TOKEN);
    }

    @Override
    public Date getLastAccessTime() {
        return lastAccessTime;
    }

    public void setLastAccessTime(Date lastAccessTime) {
        this.lastAccessTime = lastAccessTime;
    }


    @Override
    public IUser asUser(final IUser user) {
        IUser tmp = this.user;
        this.user = user;
        return tmp;
    }

    @Override
    public List<String> getAccessUris() {
        return accessUris;
    }

    @Override
    public List<String> asAccessUris(List<String> uris) {
        List<String> tmp=this.accessUris;
        this.accessUris = uris;
        return tmp;
    }

}
